import dayjs from "dayjs";
import { getPage, searchTaskDto, stopTask } from "@/api/idp/task";
import { type PaginationProps } from "@pureadmin/table";
import { reactive, ref, onMounted, computed } from "vue";

export function useTaskPage() {
  const dialogFormAddRef = ref();
  const searchForm = reactive<searchTaskDto>({
    name: "",
    pageNum: 1,
    pageSize: 10
  });
  const dataList = ref([]);
  const loading = ref(true);
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true
  });
  const columns: TableColumnList = [
    {
      type: "selection",
      width: 55,
      align: "left",
      hide: ({ checkList }) => checkList.includes("勾选列")
    },
    {
      label: "序号",
      type: "index",
      minWidth: 70,
      hide: ({ checkList }) => checkList.includes("序号列")
    },
    {
      label: "任务类型",
      prop: "taskType",
      cellRenderer: ({ row, props }) => (
        <el-tag
          size={props.size}
          type={row.taskType == 1 ? "danger" : "success"}
          effect="plain"
        >
          {row.taskType == 1 ? "文件检查" : "进程检查"}
        </el-tag>
      )
    },
    {
      label: "任务名称",
      prop: "name"
    },
    {
      label: "任务时间",
      formatter: ({ startTime, endTime }) => {
        return (
          dayjs(Number(startTime) * 1000).format("YYYY-MM-DD HH:mm:ss") +
          " ~ " +
          dayjs(Number(endTime) * 1000).format("YYYY-MM-DD HH:mm:ss")
        );
      }
    },
    {
      label: "任务范围",
      prop: "taskRank",
      formatter: ({ taskRank }) => {
        switch (taskRank) {
          case 1:
            return "全部设备";
          case 2:
            return "按机构发布";
          case 3:
            return "按设备发布";
        }
      }
    },
    {
      label: "任务状态",
      prop: "status",
      cellRenderer: ({ row, props }) => (
        <el-tag
          size={props.size}
          type={row.status == 1 ? "success" : "danger"}
          effect="plain"
        >
          {row.status == 1 ? "正常" : row.status == 2 ? "自动关闭" : "手动关闭"}
        </el-tag>
      )
    },
    {
      label: "创建人",
      prop: "creator",
      align: "left"
    },
    {
      label: "操作",
      fixed: "right",
      slot: "operation"
    }
  ];
  function handleDelete(row) {
    stopTask(row.id).then(() => {
      onSearch();
    });
  }
  async function onSearch() {
    loading.value = true;
    searchForm.pageNum = pagination.currentPage;
    searchForm.pageSize = pagination.pageSize;
    const r = await getPage(searchForm);
    dataList.value = r.list;
    pagination.total = r.total;
    setTimeout(() => {
      loading.value = false;
    }, 500);
  }
  function resetForm(formEl) {
    if (!formEl) return;
    formEl.resetFields();
    onSearch();
  }
  onMounted(() => {
    onSearch();
  });
  function handleSizeChange(val: number) {
    console.log(`${val} items per page`);
  }

  function handleCurrentChange(val: number) {
    console.log(`current page: ${val}`);
  }
  function handleSelectionChange(val) {
    console.log("handleSelectionChange", val);
  }
  const buttonClass = computed(() => {
    return [
      "!h-[20px]",
      "reset-margin",
      "!text-gray-500",
      "dark:!text-white",
      "dark:hover:!text-primary"
    ];
  });
  return {
    dialogFormAddRef,
    searchForm,
    dataList,
    pagination,
    loading,
    columns,
    buttonClass,
    handleDelete,
    onSearch,
    resetForm,
    handleSizeChange,
    handleCurrentChange,
    handleSelectionChange
  };
}
